package com.sprouting.ops.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.ops.annotations.invoke.IdempotentSingle;
import com.sprouting.ops.interfaces.AddGroup;
import com.sprouting.ops.interfaces.ConstantBase;
import com.sprouting.ops.interfaces.UpdGroup;
import com.sprouting.ops.model.*;
import com.sprouting.ops.model.qo.OpsDeployToolQo;
import com.sprouting.ops.service.DromaraFileStorageService;
import com.sprouting.ops.service.OpsComputerServerService;
import com.sprouting.ops.service.OpsDeployToolService;
import com.sprouting.ops.utils.CheckTool;
import com.sprouting.ops.utils.EntityTool;
import com.sprouting.ops.utils.IdTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 部署信息-工具(OpsDeployTool)(OPS_DEPLOY_TOOL) 表控制层
 *
 * @author ：lx（长沙麓谷）
 * @date 创建时间：2025-07-22 16:38:41
 */
@Slf4j
@RestController
@RequestMapping("opsDeployTool")
public class OpsDeployToolController {

    @Resource
    private OpsDeployToolService opsDeployToolService;
    @Resource
    private DromaraFileStorageService dromaraFileStorageService;
    @Resource
    private OpsComputerServerService opsComputerServerService;



    /**
     * 分页查询部署信息-工具
     * @param query 查询参数
     * @return
     */
    @PostMapping("getListPage")
    public ResultJson getListPage(@Validated OpsDeployToolQo query){
        Page page = query.getPage();
        return ResultJson.success(opsDeployToolService.getListPage(page, query));
    }

    /**
     * 删除部署信息-工具
     * @param id 部署信息-工具id
     * @return
     */
    @PostMapping("delOpsDeployTool")
    @IdempotentSingle
    public ResultJson delOpsDeployTool(String id){
        if (CheckTool.checkNull(id)){
            return ResultJson.errorCheck();
        }

        List<DromaraFileStorage> bindList = dromaraFileStorageService.getByBindId(id);

        opsDeployToolService.delOpsDeployTool(id, bindList);
        return ResultJson.success();
    }

    /**
     * 更新部署信息-工具
     * @param bo 修改参数
     * @return
     */
    @PostMapping("updOpsDeployTool")
    @IdempotentSingle
    public ResultJson updOpsDeployTool(@Validated(UpdGroup.class) OpsDeployTool bo){
        String msg = checkOpsDeployTool(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }

        // 提取Markdown的数据
        // 修改的时候也要提取，然后转换为生产的数据，这里要注意的是，修改的时候，不考虑去删除旧的数据的问题，到时候删除的时候，直接使用objId统一处理即可，因为这个数据量还是比较小的，没必要找的旧的去删，新的去加，搞麻烦了
        List<DromaraFileStorage> list = getMarkdownImage(bo);

        EntityTool.updBase(bo);
        opsDeployToolService.updOpsDeployTool(bo, list);
        return ResultJson.success(bo);
    }

    /**
     * 保存部署信息-工具
     * @param bo 保存参数
     * @return
     */
    @PostMapping("saveOpsDeployTool")
    @IdempotentSingle
    public ResultJson saveOpsDeployTool(@Validated(AddGroup.class) OpsDeployTool bo){
        String msg = checkOpsDeployTool(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }

        bo.setId(IdTool.getId());
        EntityTool.saveBase(bo);

        // 补充场所id
        OpsComputerServer server = opsComputerServerService.getById(bo.getComputerServerId());
        bo.setSiteId(server.getSiteId());

        // 提取Markdown的数据
        List<DromaraFileStorage> list = getMarkdownImage(bo);

        opsDeployToolService.saveOpsDeployTool(bo, list);
        return ResultJson.success(bo);
    }

    /**
     * 获取图片信息
     * @param opsDeployTool 工具部署信息
     * @return 提取到的图片信息用于转正式
     */
    private List<DromaraFileStorage> getMarkdownImage(OpsDeployTool opsDeployTool) {
        List<DromaraFileStorage> list = new ArrayList<>();
        List<DromaraFileStorage> startEndlList = dromaraFileStorageService.getMarkdownImagesUrlToFileStorage(opsDeployTool.getStartEnd(), opsDeployTool.getId(), ConstantBase.FileSourceType.TOOL_DEPLOY_START_END);
        if (CollUtil.isNotEmpty(startEndlList)){
            list.addAll(startEndlList);
        }
        List<DromaraFileStorage> deployProcesstList = dromaraFileStorageService.getMarkdownImagesUrlToFileStorage(opsDeployTool.getDeployProcess(), opsDeployTool.getId(), ConstantBase.FileSourceType.TOOL_DEPLOY_PROCESS);
        if (CollUtil.isNotEmpty(deployProcesstList)){
            list.addAll(deployProcesstList);
        }
        List<DromaraFileStorage> verificationList = dromaraFileStorageService.getMarkdownImagesUrlToFileStorage(opsDeployTool.getVerification(), opsDeployTool.getId(), ConstantBase.FileSourceType.TOOL_DEPLOY_VERIFICATION);
        if (CollUtil.isNotEmpty(verificationList)){
            list.addAll(verificationList);
        }

        return list;
    }

    /**
     * 部署信息-工具数据校验
     * @param bo 部署信息-工具 校验数据
     * @return 非null则有错误
     */
    private String checkOpsDeployTool(OpsDeployTool bo){

        return null;
    }

}
